package com.bl.student.task.job;

import com.bl.student.common.utils.DateUtils;
import com.bl.student.dao.model.*;
import com.bl.student.dao.repository.ApplyRepository;
import com.bl.student.dao.repository.ConfirmRepository;
import com.bl.student.dao.repository.TaskRepository;
import com.bl.student.service.biz.task.impl.TaskServiceImpl;
import com.bl.student.service.vo.comment.CommentAddVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * Created by liangweikang on 2018/3/16.
 */
@Log4j2
@Component
@EnableScheduling
public class ArenaScheduleJob {

    @Autowired
    TaskRepository taskRepository;

    @Autowired
    ApplyRepository applyRepository;

    @Autowired
    ConfirmRepository confirmRepository;

    @Autowired
    TaskServiceImpl taskService;

    final static private int ARENA_SETTLE_PAGE_SIZE = 100;

    public void execute() {
        log.info("schedule arena at " + DateUtils.formatDate(new Date(System.currentTimeMillis()), DateUtils.FORMAT_YYYY_MM_DD_HH_MM_SS));
        long now = System.currentTimeMillis();

        // deadline检测
        List<TaskEntity> taskEntities = taskRepository.findByStatus("ONLINE");
        List<TaskEntity> taskEntities1 = taskRepository.findByStatus("BFINISH");
        taskEntities.addAll(taskEntities1);

        for (TaskEntity taskEntity : taskEntities) {
            int deadLine = taskEntity.getDeadline();
            log.info("taskEntity.getId " + taskEntity.getId());
            List<ApplyEntity> applyEntityList = applyRepository.findByTaskIdAndStatus(taskEntity.getId(), 1);
            for (ApplyEntity applyEntity : applyEntityList) {
                log.info("applyEntity.getId ====" + applyEntity.getApplyId());
                List<ConfirmEntity> confirmEntities = confirmRepository.findByTaskIdAndPartybIdOrderByCreateTimeDesc(taskEntity.getId(), applyEntity.getPartybId());
                log.info("confirmEntities.size ====" + confirmEntities.size());
                log.info("time gap === " + calculateTimeGapDay(applyEntity.getStartTime(), new Date()));
                if ((confirmEntities == null || confirmEntities.size() == 0) && (calculateTimeGapDay(applyEntity.getStartTime(), new Date()) >= deadLine)) {
                    applyEntity.setStartRefund(1);
                    applyEntity.setUpdateTime(new Date());
                    applyRepository.save(applyEntity);
                }
            }
        }

        // 14天检测
        List<TaskEntity> taskEntities2 = taskRepository.findByStatus("BFINISH");
        for (TaskEntity taskEntity : taskEntities2) {
            List<ApplyEntity> applyEntityList = applyRepository.findByTaskIdAndStatus(taskEntity.getId(), 1);
            for (ApplyEntity applyEntity : applyEntityList) {
                List<ConfirmEntity> confirmEntities = confirmRepository.findByTaskIdAndPartybIdOrderByCreateTimeDesc(taskEntity.getId(), applyEntity.getPartybId());
                if ((confirmEntities != null && confirmEntities.size() > 0) && (calculateTimeGapDay(applyEntity.getStartTime(), new Date()) >= 14)) {
                    // 甲方给乙方评论
                    CommentAddVo commentAddVo = new CommentAddVo();
                    commentAddVo.setContent("很棒，辛苦啦~");
                    commentAddVo.setPoint(5.0);
                    commentAddVo.setTaskId(taskEntity.getId());
                    commentAddVo.setReceiverId(applyEntity.getPartybId());
                    commentAddVo.setType("PARTYA");
                    taskService.addComment(commentAddVo, taskEntity.getPartyaId());
                }
            }
        }

    }

    /** 比较两个时间相差天数 */
    public static float calculateTimeGapDay(Date time1, Date time2) {
        float day = 0;
        try {
            Date date1, date2;
            date1 = time1;
            date2 = time2;
            long millisecond = date2.getTime() - date1.getTime();
            day = millisecond / (24 * 60 * 60 * 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (day);
    }


}